/**
 * Project: lionbule-other
 * 
 * File Created at 2012-3-29
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.verfy.parser;

/**
 * OpenVerfyParser.
 * <p>
 * GE:greate or equal LE:less or equal G:greate L:less
 * </p>
 * <p>
 * character for infinity : [∞]
 * </p>
 * 
 * @author LionBule 2012-3-29 上午11:01:32
 */
public class NumberExpression implements Expression {

    public static final int G_AND_L   = 1;
    public static final int G_AND_LE  = 2;
    public static final int GE_AND_L  = 3;
    public static final int GE_AND_LE = 4;

    public static final int L_OR_G    = 5;
    public static final int L_OR_GE   = 6;
    public static final int LE_OR_G   = 7;
    public static final int LE_OR_GE  = 8;

    public static final int GREATE    = 9;
    public static final int LESS      = 10;
    public static final int GE        = 11;
    public static final int LE        = 12;

    private double          left;
    private double          right;
    private int             flag;

    public NumberExpression(Double left, Double right, int flag) {
        this.flag = flag;
        this.left = left == null ? 0 : left.doubleValue();
        this.right = right == null ? 0 : right.doubleValue();
    }

    public NumberExpression(Double value, int flag) {
        if (flag == 9 || flag == 11) {
            this.left = value == null ? 0 : value;
        } else if (flag == 10 || flag == 12) {
            this.right = value == null ? 0 : value;
        } else {
            throw new ExpressionException("constructed function: no support this expression. flag="
                    + flag);
        }
        this.flag = flag;
    }

    @Override
    public boolean matche(Object data) {
        if (data == null) {
            return true;
        }

        if (!(data instanceof Number)) {
            throw new ExpressionException(
                    "NumberExpression.matche, the type of data is not number.");
        }

        double tmpData = Double.valueOf(data.toString());
        switch (flag) {
            case G_AND_L: {
                return tmpData > left && tmpData < right;
            }
            case G_AND_LE: {
                return tmpData > left && tmpData <= right;
            }
            case GE_AND_L: {
                return tmpData >= left && tmpData < right;
            }
            case GE_AND_LE: {
                return tmpData >= left && tmpData <= right;
            }
            case L_OR_G: {
                return tmpData < left || tmpData > right;
            }
            case L_OR_GE: {
                return tmpData < left || tmpData >= right;
            }
            case LE_OR_G: {
                return tmpData <= left || tmpData > right;
            }
            case LE_OR_GE: {
                return tmpData <= left || tmpData >= right;
            }
            case GREATE: {
                return tmpData > left;
            }
            case LESS: {
                return tmpData < right;
            }
            case GE: {
                return tmpData >= left;
            }
            case LE: {
                return tmpData <= right;
            }

            default: {
                throw new ExpressionException("no support this expression. flag=" + flag);
            }
        }
    }

}
